package com.jtjrenren.android.taxi.driver.manger.service;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.support.annotation.Nullable;

import com.baidu.location.BDLocation;
import com.igexin.sdk.PushManager;
import com.jtjrenren.android.taxi.driver.Constants;
import com.jtjrenren.android.taxi.driver.manger.AppManger;
import com.jtjrenren.android.taxi.driver.manger.push.cmd.ControlCmd;
import com.jtjrenren.android.taxi.driver.utils.ThreadManger;
import com.jtjrenren.android.taxi.driver.manger.task.CheckYydTask;
import com.library.utils.LogUtil;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by Administrator on 2016/2/26.
 */
public class MainService extends Service {
    private static final int WHAT_SEND_LOC = 1; //发送定位
    private static final int REFRESH_TIME_SENDLOC = 15 * 1000; // 秒

    private static final int WHAT_REFRESH_YYDCHECK = 2; //检查预约单
    private static final int REFRESH_TIME_YYDCHECK = 3 * 60 * 1000; // 秒

    private static final int WHAT_REFRESH_GT = 3; //检查预约单
    private static final int REFRESH_TIME_GT = 60 * 1000; // 秒

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        if (AppManger.getInstance().isSendLoc())
            startSendLoc();
//        handler.sendEmptyMessageDelayed(WHAT_REFRESH_GT, REFRESH_TIME_GT);
        return new LocalBinder();
    }

    @Override
    public boolean onUnbind(Intent intent) {
        return super.onUnbind(intent);
    }

    public class LocalBinder extends Binder {
        public MainService getMainService() {
            return MainService.this;
        }
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case WHAT_SEND_LOC:
                    LogUtil.log("service", "上传定位:what");
                    handler.removeMessages(WHAT_SEND_LOC);
                    handSendLocation();
                    handler.sendEmptyMessageDelayed(WHAT_SEND_LOC, REFRESH_TIME_SENDLOC);
                    break;
                case WHAT_REFRESH_YYDCHECK:
                    handler.removeMessages(WHAT_REFRESH_YYDCHECK);
                    ThreadManger.getInstance().exec(new CheckYydTask(getApplicationContext()));
                    handler.sendEmptyMessageDelayed(WHAT_REFRESH_YYDCHECK, REFRESH_TIME_YYDCHECK);
                    break;
                case WHAT_REFRESH_GT:
                    handler.removeMessages(WHAT_REFRESH_GT);
                    boolean pushTurnedOn = PushManager.getInstance().isPushTurnedOn(getApplicationContext());
                    LogUtil.log("service", "检查个推服务:" + pushTurnedOn);
//                    if (!pushTurnedOn) {
                        LogUtil.log("service", "个推初始化");
                        PushManager.getInstance().initialize(getApplicationContext());
//                    }
                    handler.sendEmptyMessageDelayed(WHAT_REFRESH_GT, REFRESH_TIME_GT);
                    break;
            }
        }
    };

    public void startSendLoc() {
        LogUtil.log("service", "开启上传定位");
        handler.sendEmptyMessage(WHAT_SEND_LOC);
    }

    public void requestSendLoc() {
        LogUtil.log("service", "立刻上传定位");
        handler.removeMessages(WHAT_SEND_LOC);
        handler.sendEmptyMessage(WHAT_SEND_LOC);
    }

    public void stopSendLoc() {
        handler.removeMessages(WHAT_SEND_LOC);
    }

    public void startRefreshYYD() {
        handler.sendEmptyMessage(WHAT_REFRESH_YYDCHECK);
    }

    private void handSendLocation() {
        LogUtil.log("service", "立刻上传定位:handSendLocation");
        BDLocation bdLocation = AppManger.getInstance().currentLocation;
        if (bdLocation == null)
            return;

        boolean isGps = bdLocation.getLocType() == BDLocation.TypeGpsLocation;

//        int carStatus = AppManger.getInstance().driver.getCarStatus();
        int carStatus = Constants.STATUS_CAR_KC;

        float speed = bdLocation.getSpeed();
        int direction = (int) (bdLocation.getDirection() / 2);
        String time = new SimpleDateFormat("yyMMddHHmmss").format(new Date());
        String pushPositionMsg = ControlCmd.getPushPositionMsg(isGps, carStatus, bdLocation.getLatitude() + "", bdLocation.getLongitude() + "", speed, direction, time);
        AppManger.getInstance().sendMsg(pushPositionMsg);
    }
}
